package itheima04;

import utils.C3P0Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
    测试C3P0Util工具类
    步骤:
        1.使用C3P0Util工具类获取连接Connection对象
        2.定义sql语句,参数使用?代替
        3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        4.PreparedStatement对象调用方法,给?赋值
        5.PreparedStatement对象执行增删改查的方法,获取结果
        6.处理结果
        7.关闭资源
 */
/*public class Demo06C3P0UtilQuery {
    public static void main(String[] args) throws Exception {
        //1.使用C3P0Util工具类获取连接Connection对象
        Connection con = C3P0Util.getConnection();

        //2.定义sql语句,参数使用?代替
        String sql = "select * from users where uid=?";

        //3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        PreparedStatement pstmt = con.prepareStatement(sql);

        //4.PreparedStatement对象调用方法,给?赋值
        pstmt.setObject(1,"u001");

        //5.PreparedStatement对象执行增删改查的方法,获取结果
        ResultSet rs = pstmt.executeQuery();

        //6.处理结果
        if(rs.next()) {
            Object uid = rs.getObject("uid");
            Object uname = rs.getObject("uname");
            Object upass = rs.getObject("upass");
            System.out.println("编号: "+uid+", 用户名: "+uname+", 密码: "+upass);
        } else  {
            System.out.println("没有查询到结果");
        }
        //7.关闭资源
        C3P0Util.release(con,pstmt,rs);
    }
}*/

public class Demo06C3P0UtilQuery {
    public static void main(String[] args) throws SQLException {
        //1.使用C3P0Util工具类获取连接Connection对象
        Connection connection=C3P0Util.getConnection();
        //2.定义sql语句,参数使用?代替
        String sql="select * from users where uid=?";
        //3.Connection对象获取执行sql语句的PreparedStatement对象,传递sql语句
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4.PreparedStatement对象调用方法,给?赋值
        preparedStatement.setObject(1,"u005");
        ResultSet resultSet=preparedStatement.executeQuery();
        //5.PreparedStatement对象执行增删改查的方法,获取结果
        if(resultSet.next()){
            Object uid=resultSet.getObject("uid");
            Object uname = resultSet.getObject("uname");
            Object upass = resultSet.getObject("upass");
            System.out.println("编号:"+uid+",用户名:"+uname+",密码:"+upass);
        }else {
            System.out.println("没有查询结果");
        }
        //6.处理结果
        C3P0Util.release(connection,preparedStatement,resultSet);
        //7.关闭资源
    }
}